package com.bcxk.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bcxk.common.Respond;
import com.bcxk.entity.dto.Cron;
import com.bcxk.entity.dto.Order;
import com.bcxk.order.redis.RedisService;
import com.bcxk.order.scheduling.TimeTasks;
import com.bcxk.order.ulit.OrderCodeFactory;
import com.bcxk.service.CronService;
import com.bcxk.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    CronService cronService;
    @Autowired
    RedisService redisService;

    @RequestMapping("setCron")
    public void setCron() {
        Cron crons = cronService.getCron(1L);
        TimeTasks.setCron(crons.getCron());
    }

    @PostMapping("save")
    public Respond addOrder(Order order) {
        String orderCode = OrderCodeFactory.getOrderCode(order.getMerchantId());
        order.setOrderNo(Long.valueOf(orderCode));
        orderService.save(order);
        redisService.set(orderCode, orderCode, 60 * 30L );
        return Respond.ok(orderCode);
    }
    @PostMapping("update")
    public Respond updateOrder(String OrderNO ) {
        orderService.orderUpdate(OrderNO ,0);
        return Respond.ok();
    }

    @PostMapping("orderByid")
    public Respond orderById(@RequestParam("id") long id) {

        return Respond.ok(orderService.getById(11));
    }

    @PostMapping("updateorderByid")
    public Respond updateOrder(@RequestParam("orderno") String orderNo, @RequestParam("state") long state) {
        orderService.orderUpdate(orderNo, state);

        return Respond.ok(orderService.getById(11));
    }


    @RequestMapping("getOrderByMerchant")
    public Respond addOrder(@RequestParam("merchantId") long merchantId,
                            @RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNUm) {
        return Respond.ok(orderService.getOrderByMerchant(merchantId, pageNUm));
    }

    @RequestMapping("getOrderByCondition")
    public Respond getOrderByCondition(@RequestParam("merchantId") long merchantId,
                                       @RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNUm,
                                       @RequestParam(value = "text", required = false) String text,
                                       @RequestParam(value = "starttime", required = false) String startTime,
                                       @RequestParam(value = "endtime", required = false) String endTime,
                                       @RequestParam(value = "state", required = false) Long state,
                                       @RequestParam(value = "timetype", required = false) int timeType
    ) {
        IPage ipage = null;
        switch (timeType) {
            /*根据时间类型来获取值*/
            case 1:
                ipage = orderService.getOrderByCreateTime(merchantId, pageNUm, startTime, endTime, text, state);
            case 2:
                ipage = orderService.getOrderByInTime(merchantId, pageNUm, startTime, endTime, text, state);
            case 3:
                ipage = orderService.getOrderByOutTime(merchantId, pageNUm, startTime, endTime, text, state);
        }
        return Respond.ok(ipage);
    }

    @RequestMapping("addOrderTest")
    public Respond addOrderText() {
        String statrdate = "2019/09/10 14:00:00";
        String createdate = "2018/08/08 14:22:10";
        String enddate = "2019/09/20 14:00:00";

        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
        List<Order> orders = new ArrayList<>();
        for (int i = 500; i < 1000; i++) {
            Order order = new Order();
            order.setMerchantId(1L);
            order.setRoomId(1);
            order.setOrderNo(i);
            order.setName("测试数据" + i);
            order.setCheckOutTime(enddate);
            order.setTotalPrices(100000);
            order.setDiscounts(0);
            order.setPay(100000);
            order.setAmountReceived(100000);
            order.setRemark("当前订单" + i);
            order.setCheckInTime(statrdate);
            order.setState(1);
            order.setBrokerage(0);
            order.setUsername("test");
            order.setCreatTime(createdate);
            orders.add(order);

        }
        orderService.saveBatch(orders, orders.size());

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        //获取当前系统时间
        sf.format(new Date());
        return Respond.ok("11");
    }


}
